package F21AS_Freddy.AFJ_Taxikiosk.src;

import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.util.Iterator;

/**
 * This is the manager class
 * @author Freddy Wong
 * F21AS Course Work Stage2 @ Year 2015
 */
public class TrainingDataManager implements WindowListener {

	private Queue queue;
	private Kiosk kiosk;

	public TrainingDataManager(Queue queue, Kiosk kiosk) {
		this.queue= queue;
		this.kiosk = kiosk;

	}

	// handle the window close event
	@Override
	public void windowClosing(WindowEvent arg0) {

		Thread thread = new Thread(new Runnable() {
			@Override
			public void run() {
				{
					// Closed the Kiosk
					synchronized (kiosk) {
						kiosk.setIsOpen(false);
					}
					// close the queue and remove any remaining passengers
					synchronized (queue) {

						queue.setIsOpen(false);
						if (!queue.getIsEmpty()) {

							queue.setStatus("Queue closed - logging remaining passengers");
							try {
								Thread.sleep(2000);
							} catch (InterruptedException e) {
								e.printStackTrace();
							}
							for (Iterator<Passenger> it = queue.iterator();
									it.hasNext();) {

								try {
									Thread.sleep(500);
								} catch (InterruptedException e) {
									e.printStackTrace();
								}

							}

						}

						System.exit(0);
					}
				}

			}
		});
		thread.start();

	}

	@Override
	public void windowActivated(WindowEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void windowClosed(WindowEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void windowDeactivated(WindowEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void windowDeiconified(WindowEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void windowIconified(WindowEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void windowOpened(WindowEvent arg0) {
		// TODO Auto-generated method stub
		
	}

} // End of TrainingDataManager Class
